'use strict';
(function() {

    /**
     * angularjs的ng-repeat指令只识别数组
     */
    function _range(number) {
        number = parseInt(number);
        if(number < 1){
            number = 1;
        }
        return new Array(number);
    }

    /**
     * 初始化翻页信息
     */
    function initShowPage($scope) {
        var showPageCnt = 7;
        var firstPage = 1;
        var lastPage = showPageCnt;

        var pagin = $scope.pagin;
        if (pagin.pageCnt <= showPageCnt) {
            lastPage = pagin.pageCnt;
            showPageCnt = pagin.pageCnt;

        } else {
            var isEven = showPageCnt % 2 == 0;
            firstPage = pagin.pageIndex - (parseInt(showPageCnt / 2) - (isEven ? 1 : 0));
            lastPage = pagin.pageIndex + parseInt(showPageCnt / 2);
            if (firstPage < 1) {
                lastPage += 1 - firstPage;
                firstPage = 1;
            } else if (lastPage > pagin.pageCnt) {
                firstPage -= lastPage - pagin.pageCnt;
                lastPage = pagin.pageCnt;
            }
        }

        $scope._baseCnt = firstPage;
        $scope._showPageCnt = showPageCnt;
    }

    /**
     * 翻页方法，过滤后再调用实际翻页方法
     */
    function _showPage(pageIndex, isParent) {
        var $scope = isParent ? this : this.$parent;

        if (pageIndex < 1 || pageIndex > $scope.pagin.pageCnt || $scope.pagin.pageIndex == pageIndex) {
            return;
        } else {
            $scope.pagin.pageIndex = pageIndex;

            $scope.show();
            initShowPage($scope);
        }
    }



    function initPagin($scope) {
        var recordCnt = parseInt($scope.pagin.recordCnt);
        var pageSize = parseInt($scope.pagin.pageSize);
        pageSize = !pageSize || pageSize < 1 ? 1 : pageSize;
        recordCnt = !recordCnt ? 0 : recordCnt;

        $scope.pagin.recordCnt = recordCnt;
        $scope.pagin.pageSize = pageSize;

        var pageCnt = parseInt(recordCnt / pageSize) + (recordCnt % pageSize == 0 ? 0 : 1);
        if(pageCnt < 1){
            pageCnt = 1;
        }
        $scope.pagin.pageCnt = pageCnt;
    }


    ngApp.directive("scPagin", ['AppConfigConstant', function(AppConfigConstant) {
        var appBasePath = AppConfigConstant.APP_BASE_PATH;
        return {
            'restrict': 'E',
            'replace': false,
            scope: {
                pagin:'=',
                show: "&",
                visible: "="
            },
            templateUrl: appBasePath + "pagin/directive/sc-pagin/view/sc-pagin.html",
            controller: function($scope, $element) {
                $scope._range = _range;
                $scope._showPage = _showPage;
                // $scope.visible = false;//始终显示

                $scope.$watch('pagin', function(newValue, oldValue) {
                    if(!$scope.inited){
                        $scope.inited = true;
                        initPagin($scope);
                        initShowPage($scope);

                    }else if (newValue.recordCnt !== oldValue.recordCnt || newValue.pageSize !== oldValue.pageSize) {
                        initPagin($scope);
                        initShowPage($scope);
                    }
                }, true);//false不监视引用，true为全等监视，会监视引用
            }
        };
    }]);

})();